package com.bee.littlebeetravel.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.activity.MainActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class FriendFragment extends Fragment {

    private Unbinder unbinder;

    public Fragment[] mFragments;

    @BindView(R.id.fl_content)
    FrameLayout fl_content;
    @BindView(R.id.tv_message)
    TextView tv_message;
    @BindView(R.id.tv_name_list)
    TextView tv_name_list;
    @BindView(R.id.tv_track)
    TextView tv_track;
    public int friendIndex;

    @OnClick({R.id.tv_message, R.id.tv_name_list, R.id.tv_track})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_message:
                tv_message.setTextColor(getResources().getColor(R.color.black));
                tv_name_list.setTextColor(getResources().getColor(R.color.gray999));
                tv_track.setTextColor(getResources().getColor(R.color.gray999));
                setIndexSelected(0);
                break;
            case R.id.tv_name_list:
                tv_message.setTextColor(getResources().getColor(R.color.gray999));
                tv_name_list.setTextColor(getResources().getColor(R.color.black));
                tv_track.setTextColor(getResources().getColor(R.color.gray999));
                setIndexSelected(1);
                break;
            case R.id.tv_track:
                tv_message.setTextColor(getResources().getColor(R.color.gray999));
                tv_name_list.setTextColor(getResources().getColor(R.color.gray999));
                tv_track.setTextColor(getResources().getColor(R.color.black));
                setIndexSelected(2);
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_friend, container, false);
        unbinder = ButterKnife.bind(this, v);
        initFragments();
        return v;
    }

    // 初始化fragment
    private void initFragments() {
        FriendMessageFragment friendMessageFragment = new FriendMessageFragment();
        FriendNameListFragment friendNameListFragment = new FriendNameListFragment();
        FriendTrackFragment friendTrackFragment = new FriendTrackFragment();
        mFragments = new Fragment[]{friendMessageFragment, friendNameListFragment, friendTrackFragment};
        FragmentTransaction ft =
                getChildFragmentManager().beginTransaction();
        ft.add(R.id.fl_content, friendMessageFragment).commit();
        setIndexSelected(0);
    }

    // 设置显示指定的fragment
    public void setIndexSelected(int index) {
        if (friendIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //隐藏
        ft.hide(mFragments[friendIndex]);
        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.fl_content, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }
        // 异步防止频繁操作崩溃
        fragmentManager.executePendingTransactions();
        ft.commit();
        //再次赋值
        friendIndex = index;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}